package view.timeline;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;

import constants.Icons;
import controller.CompleteListener;


import view.BaseComponent;

public class TimelineCommandPanel extends BaseComponent implements ActionListener {

	private boolean playing;
	private boolean rewinding;
	
	private JButton start;
	private JButton end;
	
	private JButton play;
	private JButton rewind;
	
	public TimelineCommandPanel() {
		playing = false;
		rewinding = false;
		
		start = new JButton(Icons.getIcon("start"));
		end = new JButton(Icons.getIcon("end"));
		play = new JButton(Icons.getIcon("play"));
		rewind = new JButton(Icons.getIcon("rewind"));
		
		start.setActionCommand("start");
		end.setActionCommand("end");
		play.setActionCommand("play");
		rewind.setActionCommand("rewind");
		
		start.setToolTipText("Goes to the beginning");
		end.setToolTipText("Goes to the end");
		play.setToolTipText("Plays the strat");
		rewind.setToolTipText("rewind the strat");
		
		play.addActionListener(this);
		rewind.addActionListener(this);
		
		this.setLayout(new BoxLayout(this,BoxLayout.LINE_AXIS));
		this.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
		
		this.add(start);
		this.add(Box.createHorizontalStrut(3));
//		this.add(rewind);
//		this.add(Box.createHorizontalStrut(3));
		this.add(play);
		this.add(Box.createHorizontalStrut(3));
		this.add(end);
		
		start.setMinimumSize(new Dimension(30,20));
		rewind.setMinimumSize(new Dimension(30,20));
		play.setMinimumSize(new Dimension(30,20));
		end.setMinimumSize(new Dimension(30,20));
		
		start.setMaximumSize(new Dimension(30,20));
		rewind.setMaximumSize(new Dimension(30,20));
		play.setMaximumSize(new Dimension(30,20));
		end.setMaximumSize(new Dimension(30,20));
		
		this.setMaximumSize(new Dimension(150,50));
	}
	
	public void setPlaying(boolean playing) {
		if (play.getActionCommand().equals("pause"))
			play.doClick();
	}
	
	@Override
	public void attachListener(CompleteListener l) {
		start.addActionListener(l);
		end.addActionListener(l);
		play.addActionListener(l);
		rewind.addActionListener(l);
	}

	@Override
	public void setEnabled(boolean enable) {
		start.setEnabled(enable);
		end.setEnabled(enable);
		play.setEnabled(enable);
		rewind.setEnabled(enable);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("play")) {
			play.setIcon(Icons.getIcon("pause"));
			playing = true;
			play.setActionCommand("pause");
		}
		if (e.getActionCommand().equals("pause")) {
			play.setIcon(Icons.getIcon("play"));
			playing = false;
			play.setActionCommand("play");
		}
//		if (e.getActionCommand().equals("rewind"))
//			switchRewindIcon();
	}

	private void switchPlayIcon() {
		if (playing)
			play.setIcon(Icons.getIcon("play"));
		else
			play.setIcon(Icons.getIcon("pause"));
		playing = !playing;
	}
	
	private void switchRewindIcon() {
		if (rewinding)
			rewind.setIcon(Icons.getIcon("rewind"));
		else
			rewind.setIcon(Icons.getIcon("pause"));
		rewinding = !rewinding;
	}
}
